package com.cwh.utils;

import javax.servlet.http.HttpServletRequest;

import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.delegate.TaskListener;
import org.apache.shiro.SecurityUtils;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.cwh.entity.ActiveUser;
import com.cwh.entity.Employee;
import com.cwh.service.EmployeeService;

public class ManagerTaskHandler implements TaskListener{

	@Override
	public void notify(DelegateTask delegateTask) {
		//spring容器
		WebApplicationContext context = ContextLoader.getCurrentWebApplicationContext();
		//获取request
		HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
		//获取当前用户主体->获取账号
		ActiveUser activeUser =(ActiveUser) SecurityUtils.getSubject().getPrincipal();
		//获取用户id
		Long userid = activeUser.getUserid();
		//获取employeeService
		EmployeeService employeeService =(EmployeeService) context.getBean("employeeService");
		//根据id查找上级
		Employee mangerEmployee = employeeService.findEmployeeById(activeUser.getManagerId());
		//设置下个流程任务办理人名字
		delegateTask.setAssignee(mangerEmployee.getName());
		System.out.println("执行了："+mangerEmployee.getName());
	}

}
